package utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;

import org.springframework.web.multipart.MultipartFile;

import com.aliyun.openservices.oss.OSSClient;
import com.aliyun.openservices.oss.model.OSSObjectSummary;
import com.aliyun.openservices.oss.model.ObjectListing;
import com.aliyun.openservices.oss.model.ObjectMetadata;
import com.aliyun.openservices.oss.model.PutObjectResult;

/**
 * 
 * @author chenzehe
 * @desciption 上传图片到阿里云OSS
 *             http://aliyun_portal_storage.oss.aliyuncs.com/oss_api/
 *             oss_javahtml/index.html
 * 
 */
public class AliyunOSSHelper {
	static String imgServer = "http://zdmya.oss.aliyuncs.com/";
	static String bucketName = "zdmya";
	static OSSClient client = null;

	// 初始化OSSClient
	static {
		String accessKeyId = "ari600oWSk66JEDM";
		String accessKeySecret = "WXeoEmRLHvaqaTD8IPovzuWTtXX0Xo";
		client = new OSSClient(accessKeyId, accessKeySecret);
	}

	/**
	 * 上传头像
	 * 
	 * @param file
	 * @return
	 */
	public static String uploadUserAvatar(int userId, MultipartFile file) {
		String defaultAvatar = "http://zdmya.oss.aliyuncs.com/images/useratavar/userdefault.png";
		String[] fileNames = file.getOriginalFilename().split("\\.");
		String fileType = fileNames[fileNames.length - 1];
		String key = "images/useratavar/" + (userId % 100) + "/"
				+ (userId % 100) + "/" + userId + "." + fileType;
		try {
			boolean result = putObject(key, file);
			if (result) {
				return imgServer + key;
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		return defaultAvatar;
	}

	public static boolean deleteImg(String url) {
		String key = url.replace(imgServer, "");
		client.deleteObject(bucketName, key);
		return true;
	}

	/**
	 * 
	 * @param key文件名称
	 * @param file
	 * @return
	 * @throws FileNotFoundException
	 */
	private static boolean putObject(String key, MultipartFile file)
			throws FileNotFoundException {
		boolean retult = true;
		try {
			InputStream content = file.getInputStream();
			// 创建上传Object的Metadata
			ObjectMetadata meta = new ObjectMetadata();
			// 必须设置ContentLength
			meta.setContentLength(file.getSize());
			// 上传Object.
			PutObjectResult result = client.putObject(bucketName, key, content,
					meta);
			// 打印ETag
			// System.out.println(result.getETag());
		} catch (Exception e) {
			e.printStackTrace();
			retult = false;
		}
		return retult;
	}

	/**
	 * 
	 * @param key文件名称
	 * @param file
	 * @return
	 * @throws FileNotFoundException
	 */
	private static boolean putObject(String key, File file)
			throws FileNotFoundException {
		boolean retult = true;
		try {
			InputStream content = new FileInputStream(file);
			// 创建上传Object的Metadata
			ObjectMetadata meta = new ObjectMetadata();
			// 必须设置ContentLength
			meta.setContentLength(file.length());
			// 上传Object.
			PutObjectResult result = client.putObject(bucketName, key, content,
					meta);
			// 打印ETag
			System.out.println(result.getETag());
		} catch (Exception e) {
			e.printStackTrace();
			retult = false;
		}
		return retult;
	}

	/**
	 * 列出所有对象
	 */
	private static void listObjects() {
		// 获取指定bucket下的所有Object信息
		ObjectListing listing = client.listObjects(bucketName);
		// 遍历所有Object
		for (OSSObjectSummary objectSummary : listing.getObjectSummaries()) {
			System.out.println(objectSummary.getKey());
		}
	}

	public static void main(String[] args) {
		listObjects();
	}
}
